package com.ywk.mapperRegister.test.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;

//@Configuration
public class SpringContextMybatis  implements EnvironmentAware  {
	
    private Environment env;

	@Bean(name="sqlSessionFactory")
	public SqlSessionFactory sqlSessionFactory(DataSource dsMysql,ApplicationContext applicationContext) throws Exception{
		//USE logback
		org.apache.ibatis.logging.LogFactory.useSlf4jLogging();
		final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
		sessionFactory.setDataSource(dsMysql);
		sessionFactory.setMapperLocations(applicationContext.getResources("classpath:mapper/**/*.xml"));
//        sessionFactory.setConfigLocation(new ClassPathResource("config/mybatis-config.xml"));
        return sessionFactory.getObject();
	}

	@Override
	public void setEnvironment(Environment environment) {
		// TODO Auto-generated method stub
		this.env=environment;
	}
	
}
